package com.ruoyi.develop.lnGongyigongxu.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONArray;
import com.ruoyi.develop.lnGongxu.domain.LnGongxu;
import com.ruoyi.develop.lnGongxu.service.ILnGongxuService;
import com.ruoyi.develop.lnGongyi.domain.LnGongyi;
import com.ruoyi.develop.lnGongyi.service.ILnGongyiService;
import com.ruoyi.develop.lnGongyigoods.domain.LnGongyigoods;
import com.ruoyi.develop.lnGongyigoods.service.ILnGongyigoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lnGongyigongxu.domain.LnGongyigongxu;
import com.ruoyi.develop.lnGongyigongxu.service.ILnGongyigongxuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺工序关联Controller
 * @author qh
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/lnGongyigongxu/lnGongyigongxu")
public class LnGongyigongxuController extends BaseController {
    @Autowired
    private ILnGongyigongxuService lnGongyigongxuService;
    @Autowired
    private ILnGongxuService lnGongxuService;
    @Autowired
    private ILnGongyigoodsService lnGongyigoodsService;
    @Autowired
    private ILnGongyiService lnGongyiService;

    /**
     * 查询工艺工序关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:query')")
    @GetMapping("/list")
    public TableDataInfo list(LnGongyigongxu entity) {
        startPage();
        List<LnGongyigongxu> list = lnGongyigongxuService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出工艺工序关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:export')")
    @Log(title = "工艺工序关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LnGongyigongxu entity) {
        List<LnGongyigongxu> list = lnGongyigongxuService.selectList(entity);
        ExcelUtil<LnGongyigongxu> util = new ExcelUtil<LnGongyigongxu>(LnGongyigongxu. class);
        util.exportExcel(response, list, "工艺工序关联数据");
    }

    /**
     * 获取工艺工序关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lnGongyigongxuService.selectById(id));
    }

    /**
     * 新增工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:add')")
    @Log(title = "工艺工序关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LnGongyigongxu entity) {
        return toAjax(lnGongyigongxuService.insert(entity));
    }

    /**
     * 修改工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:edit')")
    @Log(title = "工艺工序关联", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LnGongyigongxu entity) {
        return toAjax(lnGongyigongxuService.update(entity));
    }

    /**
     * 修改工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:edit')")
    @Log(title = "批量修改工艺工序图纸号", businessType = BusinessType.UPDATE)
    @PutMapping("/editAll")
    public AjaxResult editAll(@RequestBody LnGongyigongxu entity) {
        return toAjax(lnGongyigongxuService.editAll(entity));
    }
    /**
     * 删除工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:remove')")
    @Log(title = "工艺工序关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lnGongyigongxuService.deleteByIds(ids));
    }

    @PostMapping("/addGx")
    public AjaxResult addGx(@Validated @RequestBody LnGongxu lnGongxuList) {
        /*String gongyiId  = request.getParameter("gongyiId");
        String coloumns = request.getParameter("choseColumns");
        JSONArray jsonArray = JSONArray.parseArray(coloumns);
        List<LnGongxu> lnGongxus= JSONArray.parseArray(jsonArray.toJSONString(), LnGongxu.class);*/
        List<LnGongxu> lnGongxus = lnGongxuList.getChoseColumns();
        String gongyiId=lnGongxuList.getGongyiId();
        for(LnGongxu lnGongxu:lnGongxus){
            List<LnGongyigongxu> lnGongyigongxuList=lnGongyigongxuService.selectGongxuGongyi(lnGongxu.getId(),gongyiId);
            if(lnGongyigongxuList.size()<1){
                LnGongyigongxu lnGongyigongxu=new LnGongyigongxu();
               // LnGongxu lnGongxu=lnGongxuService.selectById(lnGongxu.getId());
                lnGongyigongxu.setGongxuId(lnGongxu.getId());
                lnGongyigongxu.setGongyiId(gongyiId);
                lnGongyigongxu.setCode(lnGongxu.getCode());
                lnGongyigongxu.setName(lnGongxu.getName());
                lnGongyigongxu.setMoney(lnGongxu.getMoney());
                lnGongyigongxu.setFile(lnGongxu.getFile());
                lnGongyigongxu.setPhoto(lnGongxu.getPhoto());
                lnGongyigongxu.setShuoming(lnGongxu.getShuoming());
                lnGongyigongxu.setIdx(lnGongxu.getIdx());
                lnGongyigongxu.setGoodsId(lnGongxu.getGoodsId());
                lnGongyigongxu.setGoodsCode(lnGongxu.getGoodsCode());
                lnGongyigongxu.setGoodsName(lnGongxu.getGoodsName());
                lnGongyigongxu.setGoodsModel(lnGongxu.getGoodsModel());
                lnGongyigongxu.setDeptId(lnGongxu.getDeptId());
                lnGongyigongxu.setDeptName(lnGongxu.getDeptName());
                lnGongyigongxu.setWorkHour(lnGongxu.getWorkHour());
                lnGongyigongxuService.insert(lnGongyigongxu);
            }else if(lnGongyigongxuList.size()==1){
                LnGongyigongxu lnGongyigongxu=lnGongyigongxuList.get(0);
               // LnGongxu lnGongxu=lnGongxuService.selectById(lnGongxu.getId());
                lnGongyigongxu.setGongxuId(lnGongxu.getId());
                lnGongyigongxu.setGongyiId(gongyiId);
                lnGongyigongxu.setCode(lnGongxu.getCode());
                lnGongyigongxu.setName(lnGongxu.getName());
                lnGongyigongxu.setMoney(lnGongxu.getMoney());
                lnGongyigongxu.setFile(lnGongxu.getFile());
                lnGongyigongxu.setPhoto(lnGongxu.getPhoto());
                lnGongyigongxu.setShuoming(lnGongxu.getShuoming());;
                lnGongyigongxu.setIdx(lnGongxu.getIdx());
                lnGongyigongxu.setGoodsId(lnGongxu.getGoodsId());
                lnGongyigongxu.setGoodsCode(lnGongxu.getGoodsCode());
                lnGongyigongxu.setGoodsName(lnGongxu.getGoodsName());
                lnGongyigongxu.setGoodsModel(lnGongxu.getGoodsModel());
                lnGongyigongxu.setDeptId(lnGongxu.getDeptId());
                lnGongyigongxu.setDeptName(lnGongxu.getDeptName());
                lnGongyigongxu.setWorkHour(lnGongxu.getWorkHour());
                lnGongyigongxuService.update(lnGongyigongxu);
            }
        }
        return  AjaxResult.success();
    }


    @GetMapping("/addGxGyGoods")
    public AjaxResult addGxGyGoods(HttpServletRequest request) {
        String ids  = request.getParameter("ids");
        String gongyiId  = request.getParameter("gongyiId");
        for(String gongxuId:ids.split(",")){
            List<LnGongyigongxu> lnGongyigongxuList=lnGongyigongxuService.selectGongxuGongyi(gongxuId,gongyiId);
            if(lnGongyigongxuList.size()<1){
                LnGongyigongxu lnGongyigongxu=new LnGongyigongxu();
                LnGongxu lnGongxu=lnGongxuService.selectById(gongxuId);
                lnGongyigongxu.setGongxuId(gongxuId);
                lnGongyigongxu.setGongyiId(gongyiId);
                lnGongyigongxu.setCode(lnGongxu.getCode());
                lnGongyigongxu.setName(lnGongxu.getName());
                lnGongyigongxu.setMoney(lnGongxu.getMoney());
                lnGongyigongxu.setFile(lnGongxu.getFile());
                lnGongyigongxu.setPhoto(lnGongxu.getPhoto());
                lnGongyigongxu.setShuoming(lnGongxu.getShuoming());
                lnGongyigongxu.setGoodsId(lnGongyigongxu.getGoodsId());
                lnGongyigongxu.setGoodsCode(lnGongxu.getGoodsCode());
                lnGongyigongxu.setGoodsName(lnGongxu.getGoodsName());
                lnGongyigongxu.setGoodsModel(lnGongxu.getGoodsModel());
                lnGongyigongxu.setDeptId(lnGongxu.getDeptId());
                lnGongyigongxu.setDeptName(lnGongxu.getDeptName());
                lnGongyigongxu.setWorkHour(lnGongxu.getWorkHour());
                lnGongyigongxuService.insert(lnGongyigongxu);
            }else if(lnGongyigongxuList.size()==1){
                LnGongyigongxu lnGongyigongxu=lnGongyigongxuList.get(0);
                LnGongxu lnGongxu=lnGongxuService.selectById(gongxuId);
                lnGongyigongxu.setGongxuId(gongxuId);
                lnGongyigongxu.setGongyiId(gongyiId);
                lnGongyigongxu.setCode(lnGongxu.getCode());
                lnGongyigongxu.setName(lnGongxu.getName());
                lnGongyigongxu.setMoney(lnGongxu.getMoney());
                lnGongyigongxu.setFile(lnGongxu.getFile());
                lnGongyigongxu.setPhoto(lnGongxu.getPhoto());
                lnGongyigongxu.setShuoming(lnGongxu.getShuoming());
                lnGongyigongxu.setGoodsId(lnGongyigongxu.getGoodsId());
                lnGongyigongxu.setGoodsCode(lnGongxu.getGoodsCode());
                lnGongyigongxu.setGoodsName(lnGongxu.getGoodsName());
                lnGongyigongxu.setGoodsModel(lnGongxu.getGoodsModel());
                lnGongyigongxu.setDeptId(lnGongxu.getDeptId());
                lnGongyigongxu.setDeptName(lnGongxu.getDeptName());
                lnGongyigongxu.setWorkHour(lnGongxu.getWorkHour());
                lnGongyigongxuService.update(lnGongyigongxu);
            }
        }

        String goodsId  = request.getParameter("goodsId");
        List<LnGongyigoods> lnGongyigongxuList=lnGongyigoodsService.selectGoodsGongyi(goodsId, gongyiId);
        if(lnGongyigongxuList.size()<1){
            LnGongyigoods lnGongyigoods=new LnGongyigoods();
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.insert(lnGongyigoods);
        }else if(lnGongyigongxuList.size()==1){
            LnGongyigoods lnGongyigoods=lnGongyigongxuList.get(0);
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.update(lnGongyigoods);
        }

        return  AjaxResult.success();
    }

    @GetMapping("/deleteByGyGx")
    public AjaxResult deleteByGyGx(HttpServletRequest request) {
        String gongxuId  = request.getParameter("gongxuId");
        String gongyiId  = request.getParameter("gongyiId");
        lnGongyigongxuService.deleteByGyGx(gongxuId,gongyiId);
        return  AjaxResult.success();
    }
    @Log(title = "设为最后一道工序", businessType = BusinessType.DELETE)
    @GetMapping("/setLast")
    public AjaxResult setLast(HttpServletRequest request) {
        String id  = request.getParameter("id");
        String gongyiId  = request.getParameter("gongyiId");
        lnGongyigongxuService.updateAllNotLast(gongyiId);
        lnGongyigongxuService.updateLastById(id);
        return  AjaxResult.success();
    }

}
